Trò chơi Halloween vui nhộn trong UNITY Engine
10.810 lượt xem;
- MusicController.cs
- MusicScript /
- Scripts /
- Assets /
- project /
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class MusicController : MonoBehaviour {
6
7 public static MusicController instance;
8
9 private AudioSource audioSource;
10
11 void Start () {
12 MakeSingleton();
13 audioSource = GetComponent<AudioSource>();
14 }
15
16 void MakeSingleton()
17 {
18 if (instance != null)
19 {
20 Destroy(gameObject);
21 }
22 else
23 {
24 instance = this;
25 DontDestroyOnLoad(gameObject);
26 }
27 }
28
29 public void PlayMusic(bool play)
30 {
31 if (play) {
32 if (!audioSource.isPlaying)
33 {
34 audioSource.Play();
35 }
36 } else {
37 if (audioSource.isPlaying)
38 {
39 audioSource.Stop();
40 }
41 }
42 }
43
44 }
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class MusicController : MonoBehaviour {
6
7 public static MusicController instance;
8
9 private AudioSource audioSource;
10
11 void Start () {
12 MakeSingleton();
13 audioSource = GetComponent<AudioSource>();
14 }
15
16 void MakeSingleton()
17 {
18 if (instance != null)
19 {
20 Destroy(gameObject);
21 }
22 else
23 {
24 instance = this;
25 DontDestroyOnLoad(gameObject);
26 }
27 }
28
29 public void PlayMusic(bool play)
30 {
31 if (play) {
32 if (!audioSource.isPlaying)
33 {
34 audioSource.Play();
35 }
36 } else {
37 if (audioSource.isPlaying)
38 {
39 audioSource.Stop();
40 }
41 }
42 }
43
44 }